package com.shuqi.model.parse.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.text.TextUtils;

import com.shuqi.model.bean.OfferWallAppInfo;
import com.shuqi.model.bean.SoftConfigInfo;
import com.shuqi.model.bean.SoftUpdateInfo;
import com.shuqi.model.parse.HandlerBase;

/**
 * appInfo接口解析类
 * 
 * @author yanghuawei
 * */
public class GetSoftConfigInfoParser extends HandlerBase {
    private SoftConfigInfo info;
    private List<List<String>> groupList;
    private List<String> serverList;
    private List<OfferWallAppInfo> offerWallAppInfos;
    private StringBuilder webDomains;
    private List<String> webUrls;
    private boolean isError;
    private List<String> staticDomains;

    public SoftConfigInfo getParsedData() {
        return info;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) {
        if ("infos".equals(localName)) {
            info = new SoftConfigInfo();
        } else if ("offerwall".equals(localName)) {
            info.setOpenOfferWall("1".equals(isNull(atts, "status")));
        } else if ("cdncontrol".equals(localName)) {
            info.setOpenCDNRead("1".equals(isNull(atts, "status")));
        } else if ("statisticcontrol".equals(localName)) {
            if (!TextUtils.isEmpty(isNull(atts, "limit"))) {
                info.setPvLimit(Integer.valueOf(isNull(atts, "limit").trim()));
            }
        } else if ("detailpvupper".equals(localName)) {
            if (!TextUtils.isEmpty(isNull(atts, "limit"))) {
                info.setDetailPvLimit(Integer.valueOf(isNull(atts, "limit").trim()));
            }
        } else if ("detailpvday".equals(localName)) {
            if (!TextUtils.isEmpty(isNull(atts, "limit"))) {
                info.setDetailPvFailureTime(Integer.valueOf(isNull(atts, "limit").trim()));
            }
        } else if ("statUpFreq".equals(localName)) {
            if (!TextUtils.isEmpty(isNull(atts, "limit"))) {
                info.setDetailPvUpFreq(Integer.valueOf(isNull(atts, "limit").trim()));
            }
        } else if ("loading".equals(localName)) {
            info.setLoadingImgUrl(isNull(atts, "imgurl"));
        } else if ("update".equals(localName)) {
            SoftUpdateInfo updateInfo = new SoftUpdateInfo();
            updateInfo.setPushVersion(isNull(atts, "version"));
            updateInfo.setPushUrl(isNull(atts, "url"));
            String intro = isNull(atts, "intro");
            if(!TextUtils.isEmpty(intro)){
                updateInfo.setPushIntro(intro.replaceAll("br@@", "\n"));
            }
            updateInfo.setPushExtra(isNull(atts, "intro"));
            updateInfo.setPullExtra(isNull(atts, "extra"));

            updateInfo.setPullVersion(isNull(atts, "version2"));
            updateInfo.setPullUrl(isNull(atts, "url2"));
            String intro2 = isNull(atts, "intro2");
            if(!TextUtils.isEmpty(intro2)){
                updateInfo.setPullIntro(intro2.replaceAll("br@@", "\n"));
            }
            updateInfo.setPullExtra(isNull(atts, "intro2"));
            updateInfo.setPullExtra(isNull(atts, "extra2"));

            updateInfo.setShowTrial("1".equalsIgnoreCase(isNull(atts, "extra2")));
            info.setUpdateInfo(updateInfo);
        } else if ("serverlist".equals(localName)) {
            groupList = new ArrayList<List<String>>();
            info.setGroupList(groupList);
        } else if ("group".equals(localName)) {
            serverList = new ArrayList<String>();
            groupList.add(serverList);
        } else if ("server".equals(localName)) {
            serverList.add(isNull(atts, "domain"));
        } else if ("offerwallapps".equals(localName)) {
            offerWallAppInfos = new ArrayList<OfferWallAppInfo>();
            info.setOfferWallAppInfos(offerWallAppInfos);
        } else if ("offerwallapp".equals(localName)) {
            OfferWallAppInfo appInfo = new OfferWallAppInfo();
            appInfo.setId(isNull(atts, "id"));
            appInfo.setPackagename(isNull(atts, "packagename"));
            appInfo.setImgurl(isNull(atts, "imgurl"));
            offerWallAppInfos.add(appInfo);
        } else if ("squaredomains".equals(localName)) {
            webDomains = new StringBuilder();
            info.setWebDomains(webDomains);
        } else if ("squaredomain".equals(localName)) {
            webDomains.append(isNull(atts, "domain") + ",");
        } else if ("appinfo_domains".equals(localName)) {

        } else if ("appinfo_domain".equals(localName)) {
            info.setAppInfoDomains(isNull(atts, "domain"));
        } else if ("appinfo_domain".equals(localName)) {

        } else if ("weburls".equals(localName)) {
            webUrls = new ArrayList<String>();
            info.setWebUrls(webUrls);
        } else if ("weburl".equals(localName)) {
            webUrls.add(isNull(atts, "id") + "|" + isNull(atts, "url"));
        } else if ("StaticDomains".equals(localName)) {
            staticDomains = new ArrayList<String>();
            info.setStaticDomain(staticDomains);
        } else if ("StaticDomain".equals(localName)) {
            staticDomains.add(isNull(atts, "id") + "|" + isNull(atts, "domain"));
        }
        if("errorinfo".equals(localName)){
            info = new SoftConfigInfo();
            String serverTime = isNull(atts, "servertime");
            if(!TextUtils.isEmpty(serverTime)){
                try {
                    info.setServerTime(Integer.valueOf(serverTime));
                } catch (Exception e) {
                }
            }
            isError = true;
        }else{
            isError = false;
        }
    }
    /** 错误信息内容 */
    private String errorInfo;
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (isError) {
            String value = new String(ch, start, length);
            errorInfo = (errorInfo == null ? value : (errorInfo + value));
        }
    }
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if("errorinfo".equals(localName)){
            info.setErrorMessage(errorInfo);
        }
        super.endElement(uri, localName, qName);
    }
}
